package com.xy.smartsms.plugincmcc;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import org.json.JSONObject;

/**
 * Created by song on 2017/3/3.
 */

public class CMCCBeforeInfo {

    public static final String TABLE_NAME = "tb_cmcc_before";
    public static final String SQL_DROP_TABLE = " DROP TABLE IF EXISTS " + TABLE_NAME;
    public static final String SQL_CREATE_TABLE = "CREATE TABLE  IF NOT EXISTS " + TABLE_NAME + " ("
            + "id INTEGER PRIMARY KEY,"
            + " phone TEXT UNIQUE,"
            + " minReceiveTime LONG default 0,"
            + " maxReceiveTime LONG default 0,"
            + " extend TEXT" + ")";

    private static CMCCBeforeInfo instance;
    private Context mContext = null;
    private CMCCDataDBHelper mDBHelper = null;


    private CMCCBeforeInfo(Context context) {
        this.mContext = context;
        mDBHelper = new CMCCDataDBHelper(context);
    }

    public static synchronized CMCCBeforeInfo getInstance(Context context) {
        if (instance == null) {
            instance = new CMCCBeforeInfo(context);
        }
        return instance;
    }

    public JSONObject findObjectByPhone(String phone) {
        JSONObject jsonObject = null;
        Cursor cursor = null;
        try {
            SQLiteDatabase database = mDBHelper.getReadableDatabase();
            String[] cols = new String[]{
                    "id", "phone", "minReceiveTime", "maxReceiveTime"};
            cursor = database.query(TABLE_NAME, cols, "phone = ?", new String[]{phone}, null, null, null);
            jsonObject = CMCCDataDBHelper.loadSingleDataFromCursor(cols, cursor);
        } catch (Throwable e) {

        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

        return jsonObject;
    }

    public long saveOrUpdateObject(String phone, long smsReceiveTime, String extend) {
        long res = -1;
        try {
            SQLiteDatabase database = mDBHelper.getWritableDatabase();
            JSONObject jsonObject = findObjectByPhone(phone);
            if (jsonObject == null) {
                String minTime = null;
                String maxTime = null;
                if (smsReceiveTime > 0) {
                    minTime = String.valueOf(smsReceiveTime);
                    maxTime = String.valueOf(smsReceiveTime);
                } else {
                    maxTime = String.valueOf(System.currentTimeMillis() + Integer.MAX_VALUE);
                    minTime = "0";
                }

                ContentValues values = CMCCDataDBHelper.getContentValues(null, true, "phone", phone,
                        "minReceiveTime", minTime,
                        "maxReceiveTime", maxTime,
                        "extend", extend);
                if (values != null) {
                    res = database.insert(TABLE_NAME, null, values);
                }
            } else {
                ContentValues values = new ContentValues();
                if (smsReceiveTime > 0) {
                    if (smsReceiveTime < Long.valueOf(jsonObject.getString("maxReceiveTime"))) {
                        values.put("maxReceiveTime", smsReceiveTime);
                    } else if (smsReceiveTime > Long.valueOf(jsonObject.getString("minReceiveTime"))) {
                        values.put("minReceiveTime", smsReceiveTime);
                    }
                }

                if (!TextUtils.isEmpty(extend)) {
                    values.put("extend", extend);
                }

                if (values.size() > 0) {
                    database.update(TABLE_NAME, values, "phone=?", new String[]{phone});
                    res = Long.valueOf(jsonObject.getString("id"));
                }

            }
        } catch (Throwable e) {

        }

        return res;
    }
}
